package com.fast.fastenjoy.base.web.tag.model;

import com.alibaba.fastjson.JSONObject;
import com.fast.fastenjoy.base.manager.DataTranslationProvider;
import com.fast.fastenjoy.base.utils.ApplicationContextUtils;
import com.fast.fastenjoy.base.utils.JSObjectUtils;
import com.fast.fastenjoy.base.utils.StringExUtils;
import com.fast.fastenjoy.base.web.tag.BaseAuthTag;
import com.fast.fastenjoy.base.web.tag.Tags;

/**
 * 翻译标签
 * @author chenliaohua
 *
 */
public class LabelTag extends BaseAuthTag{

	private static final long serialVersionUID = -7550784658162237620L;

	private String dataProvider;
	private String options;
	private String value;
	
	@Override
	public void doTag() {
		
		String label = value;
		
		Object bean = ApplicationContextUtils.getBean(dataProvider);
		
		if(bean != null && bean instanceof DataTranslationProvider){
			String tempLabel = ((DataTranslationProvider)bean).getValue(json);
			if(StringExUtils.isNotEmpty(tempLabel)){
				label = tempLabel;
			}
		}
		
		String html = StringExUtils.$format(getTagTemplate(Tags.LABEL_TAG), "label", label);
		
		html = getBaseTagHtml(html);
		
		writeHtml(html);
	}

	@Override
	public void doInitParam() {
		json.put("value", value);
		JSONObject optJson = JSObjectUtils.isJson(options);
		if(optJson != null){
			json.putAll(optJson);
		}
	}

	public String getDataProvider() {
		return dataProvider;
	}

	public void setDataProvider(String dataProvider) {
		this.dataProvider = dataProvider;
	}

	public String getOptions() {
		return options;
	}

	public void setOptions(String options) {
		this.options = options;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

}
